package com.demo.business.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;


@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class R<T> {

    @ApiModelProperty(value = "响应码", example = "200")
    private int code;
    @ApiModelProperty(value = "响应码描述", example = "ok")
    private String msg;
    @ApiModelProperty(value = "code=200时返回的JSON响应内容")
    private T data;

    private final static int RESPONSE_CODE_ERROR_DEFAULT_VALUE = 400;
    private final static int RESPONSE_CODE_SUCCESS_DEFAULT_VALUE = 0;

    private final static String RESPONSE_MSG_SUCCESS_DEFAULT_VALUE = "操作成功";
    private final static String RESPONSE_MSG_ERROR_DEFAULT_VALUE = "操作失败，请重试";

    public static R success() {
        return R.builder().code(RESPONSE_CODE_SUCCESS_DEFAULT_VALUE).msg(RESPONSE_MSG_SUCCESS_DEFAULT_VALUE).build();
    }

    public static R success(Object data) {
        return R.builder().code(RESPONSE_CODE_SUCCESS_DEFAULT_VALUE).msg(RESPONSE_MSG_SUCCESS_DEFAULT_VALUE).data(data).build();
    }

    public static R success(String msg, Object data) {
        return R.builder().msg(msg).code(RESPONSE_CODE_SUCCESS_DEFAULT_VALUE).data(data).build();
    }

    public static R error() {
        return R.builder().code(RESPONSE_CODE_ERROR_DEFAULT_VALUE).msg(RESPONSE_MSG_ERROR_DEFAULT_VALUE).build();
    }

    public static R error(String msg) {
        return error(RESPONSE_CODE_ERROR_DEFAULT_VALUE, msg);
    }

    public static R error(int code, String msg) {
        return R.builder().code(code).msg(msg).build();
    }

    public static R error(String msg, Object data) {
        return R.builder().code(RESPONSE_CODE_ERROR_DEFAULT_VALUE).msg(msg).data(data).build();
    }
}
